package cn.iocoder.yudao.module.system.dal.mysql.scorelineconfig;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.scorelineconfig.vo.ScoreLineConfigPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.scorelineconfig.ScoreLineConfigDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 分数线配置 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface ScoreLineConfigMapper extends BaseMapperX<ScoreLineConfigDO> {

    default PageResult<ScoreLineConfigDO> selectPage(ScoreLineConfigPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ScoreLineConfigDO>()
                .likeIfPresent(ScoreLineConfigDO::getConfigName, reqVO.getConfigName())
                .eqIfPresent(ScoreLineConfigDO::getConfigKey, reqVO.getConfigKey())
                .eqIfPresent(ScoreLineConfigDO::getSubjectType, reqVO.getSubjectType())
                .eqIfPresent(ScoreLineConfigDO::getConfigType, reqVO.getConfigType())
                .eqIfPresent(ScoreLineConfigDO::getProvince, reqVO.getProvince())
                .eqIfPresent(ScoreLineConfigDO::getYear, reqVO.getYear())
                .eqIfPresent(ScoreLineConfigDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(ScoreLineConfigDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ScoreLineConfigDO::getId));
    }

    default List<ScoreLineConfigDO> selectList(ScoreLineConfigPageReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ScoreLineConfigDO>()
                .likeIfPresent(ScoreLineConfigDO::getConfigName, reqVO.getConfigName())
                .eqIfPresent(ScoreLineConfigDO::getConfigKey, reqVO.getConfigKey())
                .eqIfPresent(ScoreLineConfigDO::getSubjectType, reqVO.getSubjectType())
                .eqIfPresent(ScoreLineConfigDO::getConfigType, reqVO.getConfigType())
                .eqIfPresent(ScoreLineConfigDO::getProvince, reqVO.getProvince())
                .eqIfPresent(ScoreLineConfigDO::getYear, reqVO.getYear())
                .eqIfPresent(ScoreLineConfigDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(ScoreLineConfigDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ScoreLineConfigDO::getId));
    }

    /**
     * 根据配置键值、科目类型、省份和年份查询分数线配置
     */
    default ScoreLineConfigDO selectByConfigKeyAndSubjectTypeAndProvinceAndYear(String configKey, String subjectType, String province, Integer year) {
        return selectOne(new LambdaQueryWrapperX<ScoreLineConfigDO>()
                .eq(ScoreLineConfigDO::getConfigKey, configKey)
                .eq(ScoreLineConfigDO::getSubjectType, subjectType)
                .eq(ScoreLineConfigDO::getProvince, province)
                .eq(ScoreLineConfigDO::getYear, year)
                .eq(ScoreLineConfigDO::getStatus, 1)); // 只查询启用状态的配置
    }

    /**
     * 根据配置键值和科目类型查询分数线配置（兼容旧方法，使用默认省份和年份）
     */
    default ScoreLineConfigDO selectByConfigKeyAndSubjectType(String configKey, String subjectType) {
        return selectByConfigKeyAndSubjectTypeAndProvinceAndYear(configKey, subjectType, "安徽", 2025);
    }

    /**
     * 根据科目类型、省份和年份查询所有启用的分数线配置
     */
    default List<ScoreLineConfigDO> selectBySubjectTypeAndProvinceAndYear(String subjectType, String province, Integer year) {
        return selectList(new LambdaQueryWrapperX<ScoreLineConfigDO>()
                .eq(ScoreLineConfigDO::getSubjectType, subjectType)
                .eq(ScoreLineConfigDO::getProvince, province)
                .eq(ScoreLineConfigDO::getYear, year)
                .eq(ScoreLineConfigDO::getStatus, 1)
                .orderByDesc(ScoreLineConfigDO::getScoreValue));
    }

    /**
     * 根据科目类型查询所有启用的分数线配置（兼容旧方法，使用默认省份和年份）
     */
    default List<ScoreLineConfigDO> selectBySubjectType(String subjectType) {
        return selectBySubjectTypeAndProvinceAndYear(subjectType, "安徽", 2025);
    }

    /**
     * 查询所有启用的分数线配置
     */
    default List<ScoreLineConfigDO> selectAllEnabled() {
        return selectList(new LambdaQueryWrapperX<ScoreLineConfigDO>()
                .eq(ScoreLineConfigDO::getStatus, 1)
                .orderByAsc(ScoreLineConfigDO::getConfigType)
                .orderByDesc(ScoreLineConfigDO::getScoreValue));
    }

    /**
     * 根据配置类型查询分数线配置
     */
    @Select("SELECT * FROM system_score_line_config " +
            "WHERE config_type = #{configType} AND status = 1 AND deleted = 0 " +
            "ORDER BY score_value DESC")
    List<ScoreLineConfigDO> selectByConfigType(@Param("configType") String configType);

    /**
     * 根据配置类型、科目类型、省份和年份查询分数线配置
     */
    default List<ScoreLineConfigDO> selectByConfigTypeAndSubjectTypeAndProvinceAndYear(String configType, String subjectType, String province, Integer year) {
        return selectList(new LambdaQueryWrapperX<ScoreLineConfigDO>()
                .eq(ScoreLineConfigDO::getConfigType, configType)
                .eq(ScoreLineConfigDO::getSubjectType, subjectType)
                .eq(ScoreLineConfigDO::getProvince, province)
                .eq(ScoreLineConfigDO::getYear, year)
                .eq(ScoreLineConfigDO::getStatus, 1)
                .orderByDesc(ScoreLineConfigDO::getScoreValue));
    }

}